package com.yidianyanglao.core.interceptor;

import com.yidianyanglao.core.enums.Code;
import com.yidianyanglao.core.exception.CustomException;
import com.yidianyanglao.core.util.Log;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2016/12/26.
 */
public class BuyBackInterceptor implements HandlerInterceptor {


    /**
     * 回购方法的拦截器
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Log.info("回购或者转增方法拦截器");
        String result = (String) request.getSession().getAttribute("checkbuyback");
        if(!StringUtils.isEmpty(result)){
            if(result.equals("1")){
                throw new CustomException(Code.ERROR,"数据正在处理中，请勿重复提交");
            }
        }
        request.getSession().setAttribute("checkbuyback","1");

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 回购方法结束的拦截器
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("");
        request.getSession().setAttribute("checkbuyback","0");
        Log.info("回购或者转增处理完成");
    }
}
